﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using InSimDotNet;
using InSimDotNet.Packets;
using InSimDotNet.Out;
using System.Collections;
using System.Text.RegularExpressions;
using System.Timers;
using System.Configuration;



namespace lfsserver6
{
    public class GUI
    {
    parsers parsers = new parsers();

          public void get_user_name(string name)
        {

            if (name.Equals("all") == true)
                {
                    parsers.data_reader("all");
                }

                if (name != string.Empty)
                {
                    parsers.data_reader(name);
                }
                else
                {
                    Environment.Exit(1);
                    //application exit
                    Console.WriteLine("error");
                }
        }
        // data command function and creates UI
        public void command(InSim insim,IS_MSO mso)
        {
             if (mso.UserType == UserType.MSO_PREFIX)
              {
                 var commands = mso.Msg.Substring(mso.TextStart).Split();
               
                 if (commands.Any())
                   {
                    var command = commands.First().ToLower();

                          switch (command)
                    {
                        case "!ip":
                          
                            var name = commands.Last();
                            get_user_name(name);
                            //ArrayList ar = (ArrayList)arraylist.Clone();
                            //backgroud button
                            IS_BTN but = new IS_BTN();
                          
                            but.BStyle = ButtonStyles.ISB_DARK;
                            but.H = 115;
                            but.L = 51;
                            but.T = 51;
                            but.W = 101;
                            but.Text = "";
                            but.UCID = mso.UCID;
                            but.ReqI = 100;
                            
                            //title button
                            IS_BTN btn0 = new IS_BTN();
                            btn0.BStyle = ButtonStyles.ISB_LIGHT;
                            btn0.H = 10;
                            btn0.L = 51;
                            btn0.T = 50;
                            btn0.W = 101;
                            btn0.Text = "^6RIBANOZ SOFTWORKS: ^7[^4TOM^7]^7 IP LOGGER 2013";
                            btn0.UCID = mso.UCID;
                            btn0.ReqI = 101;
                            btn0.ClickID = 6;


                            //cancel button
                            IS_BTN can = new IS_BTN();
                            
                                       
                            can.BStyle = ButtonStyles.ISB_CLICK;
                            can.H = 10;
                            can.L = 50;
                            can.T = 167;
                            can.W = 101;
                            can.Text = "^1Cancel";
                            can.ClickID = 8;
                            can.UCID = mso.UCID;
                            can.ReqI = 44;
                            
                            Byte reqID = 102;
                            Byte klikID = 10;
                            Byte t = 60;
                            int ögöbö = 1;
                            int i;

                            insim.Send(but);
                            insim.Send(btn0);
                            insim.Send(can);
                            //reverse array. now the last index is first
                            parsers.arraylist.Reverse();
                            if (parsers.arraylist.Count > 20)
                            {
                              
                                    ArrayList range = parsers.arraylist.GetRange(0, 20);
                                    foreach (Object obj in range)
                                    {

                                        string kikki = obj.ToString();
                                        //first data button        
                                        IS_BTN button = new IS_BTN();
                                        button.BStyle = ButtonStyles.ISB_LEFT;
                                        button.H = 5;
                                        button.L = 52;

                                        for (i = 0; i < 5; i++)
                                        {
                                            button.T = t++;
                                        }

                                        button.W = 100;
                                        button.Text = "^3" + ögöbö++ + ". ^7" + kikki;
                                        button.ClickID = klikID++;
                                        button.UCID = mso.UCID;
                                        button.ReqI = reqID++;
                                        insim.Send(button);
                                    }
                                
                            }
                            else
                            {
                              
                                    foreach (Object obj in parsers.arraylist)
                                    {

                                        string kikki = obj.ToString();
                                        //first data button        
                                        IS_BTN button = new IS_BTN();
                                        button.BStyle = ButtonStyles.ISB_LEFT;
                                        button.H = 5;
                                        button.L = 52;

                                        for (i = 0; i < 5; i++)
                                        {
                                            button.T = t++;
                                        }

                                        button.W = 100;
                                        button.Text = "^3" + ögöbö++ + ". ^7" + kikki;
                                        button.ClickID = klikID++;
                                        button.UCID = mso.UCID;
                                        button.ReqI = reqID++;
                                        insim.Send(button);
                                    }
                                    //ar.Clear();
                                    parsers.arraylist.Clear();
                                    GC.Collect();
                                
                            }
                            break;

                              case "!ipinit":
                           
                            parsers.ip_parser();
                            break;

                              case "!ipexit":
                            
                            Environment.Exit(0);
                              break;
                               
                              case "!ipa":
                            var address = commands.Last();
                            //get_user_name(address);
                            parsers.ip_reader(address);
                            //ArrayList ar = (ArrayList)arraylist.Clone();
                            //backgroud button
                            IS_BTN but1 = new IS_BTN();
                            but1.BStyle = ButtonStyles.ISB_DARK;
                            but1.H = 115;
                            but1.L = 51;
                            but1.T = 51;
                            but1.W = 101;
                            but1.Text = "";
                            but1.UCID = mso.UCID;
                            but1.ReqI = 100;
                            
                            //title button
                            IS_BTN btn1 = new IS_BTN();
                            btn1.BStyle = ButtonStyles.ISB_LIGHT;
                            btn1.H = 10;
                            btn1.L = 51;
                            btn1.T = 50;
                            btn1.W = 101;
                            btn1.Text = "^6RIBANOZ SOFTWORKS: ^7[^4TOM^7]^7 IP LOGGER 2013";
                            btn1.UCID = mso.UCID;
                            btn1.ReqI = 101;
                            btn1.ClickID = 6;


                            //cancel button
                            IS_BTN can1 = new IS_BTN();
                            can1.BStyle = ButtonStyles.ISB_CLICK;
                            can1.H = 10;
                            can1.L = 50;
                            can1.T = 167;
                            can1.W = 101;
                            can1.Text = "^1Cancel";
                            can1.ClickID = 8;
                            can1.UCID = mso.UCID;
                            can1.ReqI = 44;
                            
                            Byte reqID1 = 103;
                            Byte klikID1 = 11;
                            Byte t1 = 60;
                            int ögöbö1 = 1;
                            int j;

                            insim.Send(but1);
                            insim.Send(btn1);
                            insim.Send(can1);
                            //reverse array. now the last index is first
                            parsers.arraylist.Reverse();
                            if (parsers.arraylist.Count > 20)
                            {
                              
                                    ArrayList range = parsers.arraylist.GetRange(0, 20);
                                    foreach (Object obj in range)
                                    {

                                        string kikki = obj.ToString();
                                        //first data button        
                                        IS_BTN button = new IS_BTN();
                                        button.BStyle = ButtonStyles.ISB_LEFT;
                                        button.H = 5;
                                        button.L = 52;

                                        for (j = 0; j < 5; j++)
                                        {
                                            button.T = t1++;
                                        }

                                        button.W = 100;
                                        button.Text = "^3" + ögöbö1++ + ". ^7" + kikki;
                                        button.ClickID = klikID1++;
                                        button.UCID = mso.UCID;
                                        button.ReqI = reqID1++;
                                        insim.Send(button);
                                    }
                                
                            }
                            else
                            {
                              
                                    foreach (Object obj in parsers.arraylist)
                                    {

                                        string kikki = obj.ToString();
                                        //first data button        
                                        IS_BTN button = new IS_BTN();
                                        button.BStyle = ButtonStyles.ISB_LEFT;
                                        button.H = 5;
                                        button.L = 52;

                                        for (j = 0; j < 5; j++)
                                        {
                                            button.T = t1++;
                                        }

                                        button.W = 100;
                                        button.Text = "^3" + ögöbö1++ + ". ^7" + kikki;
                                        button.ClickID = klikID1++;
                                        button.UCID = mso.UCID;
                                        button.ReqI = reqID1++;
                                        insim.Send(button);
                                    }
                                    //ar.Clear();
                                    parsers.arraylist.Clear();
                                    GC.Collect();
                            }
                                  break;

                    }
                }
            } 
        
        }

        public void deletebutton(InSim insim, IS_BTC btc)
        {
            IS_BFN bfn = new IS_BFN();
            bfn.ClickID = btc.ClickID;
            bfn.UCID = btc.UCID;
            bfn.ReqI = btc.ReqI;
            bfn.ClickID = 8;
            bfn.SubT = ButtonFunction.BFN_CLEAR;
            parsers.arraylist.Clear();
            insim.Send(bfn);
        }

        public void test()
        {
            Console.WriteLine(conf.ip);
            Console.WriteLine(conf.port);
            Console.WriteLine(conf.password);
            Console.WriteLine(conf.filename);
        }



    }
}

